package org.yoao.crm.business.impl;

import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.yoao.crm.bean.CustomerType;
import org.yoao.crm.business.service.ICustomerTypeService;
import org.yoao.crm.dao.CustomerTypeDAO;
import org.yoao.crm.struts.form.CustomerTypeForm;

/**
 * @author yxwonline.com
 * @description 
 * @date 2010-8-20 下午07:18:44
 */
public class CustomerTypeServiceImpl implements ICustomerTypeService {
	
	private CustomerTypeDAO customerTypeDAO = new CustomerTypeDAO();
	
	@Override
	public CustomerTypeForm add(CustomerTypeForm form) throws Exception {
		CustomerType customerType = new CustomerType();
		BeanUtils.copyProperties(customerType, form);
		CustomerType tmp = customerTypeDAO.findByCode(customerType.getTypeCode());
		if(tmp != null){
			form.setInfo("类型已存在,重复!");
		}else{
			int result = customerTypeDAO.insert(customerType);
			if(result > -1){
				form.setId(result);
				form.setInfo("数据保存成功.");
			}else form.setInfo("数据保存失败.");
		}
		return form;
	}

	@Override
	public void delete(String id) throws Exception {
		String[] idArr = id.split(",");
		if(idArr != null && idArr.length > 0){
			for(int i=0;i<idArr.length;i++){
				customerTypeDAO.delete(idArr[i]);
			}
		}
	}

	@Override
	public List getView() throws Exception {
		return customerTypeDAO.findAll();
	}

	@Override
	public CustomerTypeForm modify(CustomerTypeForm form) throws Exception {
		CustomerType customerType = new CustomerType();
		BeanUtils.copyProperties(customerType, form);
		if(customerTypeDAO.modify(customerType) > -1){
			form.setInfo("数据保存成功.");
		}else  form.setInfo("数据保存失败.");
		return form;
	}

	@Override
	public CustomerTypeForm getCustomerType(String id) throws Exception {
		CustomerTypeForm form = new CustomerTypeForm();
		CustomerType customerType = customerTypeDAO.findById(id);
		if(customerType != null) BeanUtils.copyProperties(form, customerType);
		return form;
	}

	@Override
	public List getCustomerType() throws Exception {
		return customerTypeDAO.findAll();
	}
}
